/*
 * @lc app=leetcode.cn id=883 lang=cpp
 *
 * [883] 三维形体投影面积
 */

// @lc code=start
class Solution
{
public:
  int projectionArea(vector<vector<int>>& grid)
  {
    int n = grid.size();
    int xy = 0, xz = 0;
    vector<int> yz(n);
    for (vector<int>& x : grid) {
      int xz_tmp = 0;
      for (int i = 0; i < x.size(); ++i) {
        if (x[i])
          ++xy;
        xz_tmp = max(xz_tmp, x[i]);
        yz[i] = max(yz[i], x[i]);
      }
      xz += xz_tmp;
    }
    int ans = xy + xz;
    for (int& h : yz)
      ans += h;
    return ans;
  }
};
// @lc code=end
